<?php

namespace App\Http\Controllers\Admin\Forum;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Forums;
use App\Models\Users;
use App\Models\Fcomments;
use App\Models\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
/**
 * 店铺控制器
 *
 * @author lxp
 * @package App\Http\Controllers\Forums
 */
class ForumsController extends BaseAdminController
{

	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 论坛列表
	 *
	 * @author yh 20200318
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function index()
	{
		// 处理排序
		$sort = request('sort', 'id');
		$order = request('order', 'desc');

		$query = Forums::orderBy($sort, $order)->where('status','!=',0);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } else {
                $query->where('nickname', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 筛选注册时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }
		// 取得列表
		$forums = $query->select([
			'id',
			'avatar',
			'title',
			'des',
			'nickname',
			'status',
			'comment_num',
			'views',
			'created_at'
		])->paginate(parent::PERPAGE);
		// 将查询参数拼接到分页链接中
		$forums->appends(app('request')->all());

		return view('admin.forum.forums', [
			'forums' => $forums,
		]);
	}
    /**
     * 论坛列表
     *
     * @author yh 20200318
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function forumshen()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Forums::orderBy($sort, $order)->where('status',0);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } else {
                $query->where('nickname', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 筛选注册时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }
        // 取得列表
        $forums = $query->select([
            'id',
            'avatar',
            'title',
            'des',
            'nickname',
            'status',
            'comment_num',
            'views',
            'created_at'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $forums->appends(app('request')->all());

        return view('admin.forum.forumshen', [
            'forums' => $forums,
        ]);
    }
	/**
	 * 添加用户
	 *
	 * @author lxp 20170825
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function add()
	{
		return view('admin.forum.forums_form');
	}
    /**
     * 编辑用户
     *
     * @author lxp 20170111
     * @param $uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function comment($id)
    {
        // 取得评论列表
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Fcomments::orderBy($sort, $order);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } else {
                $query->where('nickname', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 发布时间时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }

       $id= request('id');
        // 取得列表
        $query->where('forums_id',$id);
        $forums = $query->select([
            'id',
            'avatar',
            'content',
            'nickname',
            'forums_id',
            'title',
            'status',
            'created_at'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $forums->appends(app('request')->all());

        return view('admin.forum.comments', [
            'forums' => $forums,
            'id'=>$id,
        ]);

    }
    public function comment_add($id)
    {
        // 取得用户信息
        $user = Forums::findOrFail($id);

        return view('admin.forum.comments_form', [
            'forums' => $user,
            'id'=>$id
        ]);
    }
    public function comment_save()
    {
        // 取得用户信息
        $id = request('id');
        $user = Fcomments::findOrNew($id);
        $user->uid = Auth::user()->uid;
        $uinfo = Users::where('uid', Auth::user()->uid)->select('uid', 'nickname', 'avatar')->first();
        $user->avatar =get_file_url($uinfo ->avatar);
        $user->nickname =$uinfo ->nickname;
        $user->content = request('content');
        $user->forums_id = request('forums_id');
        $forums = Forums::where('id',request('forums_id'))->select('title')->first();
        $user->title =   $forums->title;
        $user->status = 1;
        $user->save();
        Forums::where('id',request('forums_id'))->increment('comment_num');
       return $this->success(get_session_url('index'));
    }
    /**
     * 删除讨论
     *
     * @author lxp 20160713
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Forums::destroy($id);
            Fcomments::where('forums_id', $id)->delete();
            return $this->success();
        }
    }

    /**
	 * 编辑用户
	 *
	 * @author lxp 20170111
	 * @param $uid
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function edit($id)
	{
		// 取得用户信息
		$user = Forums::findOrFail($id);

		return view('admin.forum.forums_form', [
			'user' => $user
		]);
	}
    /**
     * 审核店铺
     *
     * @author yh 20170111
     * @param $uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function shen($id)
    {
        // 取得用户信息
        $user = Forums::findOrFail($id);

        return view('admin.forum.forums_shen', [
            'user' => $user
        ]);
    }
	/**
	 * 保存用户
	 *
	 * @author lxp 20170206
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function save()
	{

		// 保存用户信息
		DB::transaction(function () {
            $rule = [

            ];
            if(!request('id')){
                $rule = array_merge([
                    'title' => 'required|unique:forums,title,' . request('id') . ',id',
                    'des' => 'required|',
                ], $rule);
            }

            $this->validate(request(), $rule, [], [

                'des' => '描述',
                'title'=>'标题',

            ]);
            $id = request('article_id', 0);
			$user = Forums::findOrNew(request('id'));
            if (!$id) {
                $user->uid = Auth::user()->uid;
                $uinfo = Users::where('uid', Auth::user()->uid)->select('uid', 'nickname', 'avatar')->first();
                $user->avatar =get_file_url($uinfo ->avatar);
                $user->nickname =$uinfo ->nickname;
            }
			$user ->title = request('title');
            $user ->des = request('des');
            $user ->is_recommend = 2;
			$user -> save();
		});
		return $this->success(get_session_url('index'));
	}

	/**
	 * 删除用户
	 *
	 * @author lxp 20160713
	 * @param int $uid
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function comment_delete($id)
	{
		if (request()->ajax() && intval($id)) {
			// 判断用户是否可以被删除
			// 删除用户
			Fcomments::destroy($id);
			return $this->success();
		}
	}
    /**
     * 审核评论
     *
     * @author yh 20170111
     * @param $uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function comment_shen($id)
    {
        // 取得用户信息
        $user = Fcomments::findOrFail($id);

        return view('admin.forum.comments_shen', [
            'user' => $user
        ]);
    }
    /**
     * 评论审核
     *
     * @author yh 20200322
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     */
    public function comment_refund()
    {
        $user = Fcomments::findOrNew(request('id'));
        $user -> status =intval(request('status'));
        $rule = [];
        if(request('status')==0){
            $rule = array_merge([
                'refund_msg' => 'required'
            ], $rule);
            $this->validate(request(), $rule, [], [
                'refund_msg'=>'审核不通过原因',
            ]);
        }
        $user -> refund_msg = request('refund_msg');
        $user->save();
        return $this->success(get_session_url('index'));
    }
    /**
     * 审核店铺
     *
     * @author yh 20200322
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     */
    public function refund()
    {
        $user = Forums::findOrNew(request('id'));
        $user -> status =intval(request('status'));
        $rule = [];
        if(request('status')==2){
            $rule = array_merge([
                'refund_msg' => 'required'
            ], $rule);
            $this->validate(request(), $rule, [], [
                'refund_msg'=>'审核不通过原因',
            ]);
        }
        $user -> refund_msg = request('refund_msg');
        $user->save();
        return $this->success(get_session_url('index'));
    }
}